package com.xuyiwei.xblog.business.system.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xuyiwei.xblog.business.system.dao.UserMapper;
import com.xuyiwei.xblog.business.system.model.User;
import com.xuyiwei.xblog.business.system.result.user.input.UserConditionVO;
import com.xuyiwei.xblog.business.system.result.user.input.UserRegister;
import com.xuyiwei.xblog.business.system.result.user.out.UserDto;
import com.xuyiwei.xblog.business.system.service.IUserService;
import com.xuyiwei.xblog.core.base.constant.enums.FileUploadType;
import com.xuyiwei.xblog.core.shiro.ShiroKit;
import com.xuyiwei.xblog.core.util.OssUtil;
import com.xuyiwei.xblog.core.util.QQUtil;
import com.xuyiwei.xblog.core.util.RSAUtil;
import com.xuyiwei.xblog.core.util.StringUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.List;
import java.util.Map;

import static com.xuyiwei.xblog.core.util.OssUtil.uploadByNetworkStream;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-02-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public int setStatus(Long userId, int status) {
        return this.baseMapper.setStatus(userId, status);
    }

    @Override
    public int setStatusIds(Long[] ids, int status) {
        return this.baseMapper.setStatusIds(ids, status);
    }

    @Override
    public int changePwd(Long userId, String pwd) {
        return this.baseMapper.changePwd(userId, pwd);
    }

    @Override
    public List<UserDto> findPageByCondition(Page<UserDto> page, @Param("vo") UserConditionVO vo, String orderByField, boolean isAsc) {
        return this.baseMapper.findPageByCondition(page,vo,orderByField,isAsc);
    }

    @Override
    public int setRoles(Long userId, String roleIds) {
        return this.baseMapper.setRoles(userId, roleIds);
    }

    @Override
    public User getByAccount(String account) {
        return this.baseMapper.getByAccount(account);
    }

    @Override
    public User addLongCount(String account) {
        User param = new User();
        param.setAccount(account);
        User user  = this.baseMapper.getUserByParam(param);
        if(user!=null){
             int longCount = (user.getLoginCount()==null?0:user.getLoginCount())+1;
             user.setLoginCount(longCount);
             user.updateById();
        }
        return null;
    }

    @Override
    public void registerUser(@Valid UserRegister userRegister) {
        String account = userRegister.getAccount();
        String password = userRegister.getPassword();
        // 完善账号信息
        User user = new User();
        BeanUtils.copyProperties(userRegister,user);

        account = RSAUtil.decryptBase64(account);
        password = RSAUtil.decryptBase64(password);
        user.setAccount(account);
        user.setPassword(password);
        user.setSalt(ShiroKit.getRandomSalt(5));
        user.setPassword(ShiroKit.md5(user.getPassword(), user.getSalt()));
        if(StringUtil.isBlank(user.getNickName())){
            user.setName("神秘人");
        }else {
            user.setName(user.getNickName());
        }
        if(user.getQq()!=null){
            Map<String, String> qqInfo =  QQUtil.getQQInfo(user.getQq());
            if(qqInfo!=null){
                String avatar = qqInfo.get("avatar");
                URL url = null;
                try {
                    url = new URL(avatar);
                    String  objectName = OssUtil.uploadByNetworkStream(url,"qq.png", FileUploadType.AVATAR);
                    user.setAvatar(objectName);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }

            }
        }
        user.insert();
    }
}
